package com.isunland.managesystem.ui;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseArrayDialogFragment;
import com.isunland.managesystem.base.BaseSelectObject;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.BaseParams;
import com.isunland.managesystem.entity.DDictionary;
import com.isunland.managesystem.entity.DDictionaryListOriginal;
import com.isunland.managesystem.entity.RSealsUseApp;
import com.isunland.managesystem.entity.SealUseQueryParams;
import com.isunland.managesystem.entity.SimpleNetworkCallBackDialogParams;
import com.isunland.managesystem.entity.TimeParams;
import com.isunland.managesystem.utils.MyDateUtil;
import com.isunland.managesystem.utils.ParamsNotEmpty;
import com.isunland.managesystem.utils.ToastUtil;
import com.isunland.managesystem.widget.SingleLineView;
import java.util.Date;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: wangyanan
 * 创建时间: 2017/6/13 18:51
 */

public class SealUseQueryFragment extends BaseQueryFragment {
  @BindView(R.id.slv_startTime) SingleLineView slvStartTime;
  @BindView(R.id.slv_endTime) SingleLineView slvEndTime;
  @BindView(R.id.slv_sealsType) SingleLineView slvSealsType;
  @BindView(R.id.slv_dataStatus) SingleLineView slvDataStatus;
  @BindView(R.id.slv_isCarryOut) SingleLineView slvIsCarryOut;
  private SealUseQueryParams mParams;

  @Override public void initData() {
    super.initData();
    mParams = (!(mBaseParams instanceof SealUseQueryParams)) ? new SealUseQueryParams()
        : (SealUseQueryParams) mBaseParams;
  }

  @Override protected BaseParams confirm() {
    Date startDate = mParams.getApplyDeptStartDate();
    Date endDate = mParams.getApplyDeptStopDate();
    //不再同一天时,结束时间不能早于开始时间
    if ((startDate != null)
        && (endDate != null)
        && (!MyDateUtil.isSameDay(startDate, endDate))
        && endDate.before(startDate)) {
      ToastUtil.show(R.string.stopDateBeforeStartDate);
      return null;
    }
    return mParams;
  }

  @Override protected void clear() {
    //更新数据
    mParams.setApplyDeptStartDate(null);
    mParams.setApplyDeptStopDate(null);
    mParams.setSealsTypeCode("");
    mParams.setSealsTypeName("全部");
    mParams.setDataStatus("");
    mParams.setDataStatusName("全部");
    mParams.setIsCarryOutCode("");
    mParams.setIsCarryOutName("不限");
    //更新页面
    slvStartTime.clear();
    slvEndTime.clear();
    slvSealsType.setTextContent("全部");
    slvDataStatus.setTextContent("全部");
    slvIsCarryOut.setTextContent("不限");
  }

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_seal_use_query, container, false);
    ButterKnife.bind(this, rootView);
    return rootView;
  }

  @Override public void initView(View view, Bundle savedInstanceState) {
    super.initView(view, savedInstanceState);
    slvStartTime.getTvContent()
        .setText(MyDateUtil.format(mParams.getApplyDeptStartDate(), MyDateUtil.TYPE_DATE));
    slvEndTime.getTvContent()
        .setText(MyDateUtil.format(mParams.getApplyDeptStopDate(), MyDateUtil.TYPE_DATE));
    slvSealsType.getTvContent().setText(mParams.getSealsTypeName());
    slvDataStatus.getTvContent().setText(mParams.getDataStatusName());
    slvIsCarryOut.getTvContent().setText(mParams.getIsCarryOutName());

    slvStartTime.getTvContent().setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        showDialog(BaseDialogFragment.newInstance(
            new TimeParams().setDate(mParams.getApplyDeptStartDate()),
            new SimpleTimeCallBackDialogFragment().setCallBack(
                new SimpleTimeCallBackDialogFragment.CallBack() {
                  @Override public void confirm(Date selectDate) {
                    if (selectDate == null) return;
                    mParams.setApplyDeptStartDate(selectDate);
                    slvStartTime.setTextContent(
                        MyDateUtil.format(selectDate, MyDateUtil.TYPE_DATE));
                  }

                  @Override public void cancel() {

                  }
                })));
      }
    });
    slvEndTime.getTvContent().setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        showDialog(
            BaseDialogFragment.newInstance(new TimeParams().setDate(mParams.getApplyDeptStopDate()),
                new SimpleTimeCallBackDialogFragment().setCallBack(
                    new SimpleTimeCallBackDialogFragment.CallBack() {
                      @Override public void confirm(Date selectDate) {
                        if (selectDate == null) return;
                        mParams.setApplyDeptStopDate(selectDate);
                        slvEndTime.setTextContent(
                            MyDateUtil.format(selectDate, MyDateUtil.TYPE_DATE));
                      }

                      @Override public void cancel() {

                      }
                    })));
      }
    });
    slvSealsType.getTvContent().setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        SimpleNetworkCallBackDialogParams params = new SimpleNetworkCallBackDialogParams();
        params.setUrl(ApiConst.getAbsoluteUrl(ApiConst.URL_QUERY_LIST_DICTIONARY));
        params.setParameters(new ParamsNotEmpty().put("memberCode", mCurrentUser.getMemberCode())
            .put("dictFlag", "oaSealsType")
            .getParams());
        params.setShowField("recordText");
        params.setClassOriginal(DDictionaryListOriginal.class);
        showDialog(BaseDialogFragment.newInstance(params,
            new SimpleNetworkCallBackDialogFragment<DDictionary>().setCallBack(
                new SimpleNetworkCallBackDialogFragment.CallBack<DDictionary>() {

                  @Override public void select(DDictionary base) {
                    if (base == null) return;
                    mParams.setSealsTypeCode(base.getRecordCode());
                    mParams.setSealsTypeName(base.getRecordText());
                    slvSealsType.setTextContent(base.getRecordText());
                  }
                })));
      }
    });
    slvDataStatus.getTvContent().setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        SimpleNetworkCallBackDialogParams params = new SimpleNetworkCallBackDialogParams();
        params.setUrl(ApiConst.getAbsoluteUrl(ApiConst.URL_QUERY_LIST_DICTIONARY));
        params.setParameters(new ParamsNotEmpty().put("memberCode", mCurrentUser.getMemberCode())
            .put("dictFlag", "flow_form_dataStatus")
            .getParams());
        params.setShowField("recordText");
        params.setClassOriginal(DDictionaryListOriginal.class);
        showDialog(BaseDialogFragment.newInstance(params,
            new SimpleNetworkCallBackDialogFragment<DDictionary>().setCallBack(
                new SimpleNetworkCallBackDialogFragment.CallBack<DDictionary>() {

                  @Override public void select(DDictionary base) {
                    if (base == null) return;
                    mParams.setDataStatus(base.getRecordCode());
                    mParams.setDataStatusName(base.getRecordText());
                    slvDataStatus.setTextContent(base.getRecordText());
                  }
                })));
      }
    });
    slvIsCarryOut.setOnClickContentListener(new View.OnClickListener() {
      @Override public void onClick(View v) {

        showDialog(SimpleArrayDialogFragment.newInstance(RSealsUseApp.getIsCarryOutList(mActivity))
            .setCallBack(new BaseArrayDialogFragment.CallBack<BaseSelectObject>() {
              @Override public void select(BaseSelectObject base) {
                if (base == null) return;
                mParams.setIsCarryOutCode(base.getCode());
                mParams.setIsCarryOutName(base.getName());
                slvIsCarryOut.setTextContent(base.getName());
              }
            }));
      }
    });
  }
}
